package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Standardlevel;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("StandardlevelDAO")
@Transactional
public class StandardlevelDAOImpl extends AbstractJpaDao<Standardlevel> implements StandardlevelDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Standardlevel.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public StandardlevelDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Standardlevel findStandardlevelById(Integer id) throws DataAccessException {

		return findStandardlevelById(id, -1, -1);
	}

	@Transactional
	public Standardlevel findStandardlevelById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardlevelById", startResult, maxRows, id);
			return (Standardlevel) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standardlevel> findStandardlevelByName(String name) throws DataAccessException {

		return findStandardlevelByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardlevel> findStandardlevelByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardlevelByName", startResult, maxRows, name);
		return new LinkedHashSet<Standardlevel>(query.getResultList());
	}

	@Transactional
	public Set<Standardlevel> findStandardlevelByNameContaining(String name) throws DataAccessException {

		return findStandardlevelByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardlevel> findStandardlevelByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardlevelByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Standardlevel>(query.getResultList());
	}

	@Transactional
	public Set<Standardlevel> findStandardlevelByDescription(String description) throws DataAccessException {

		return findStandardlevelByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardlevel> findStandardlevelByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardlevelByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Standardlevel>(query.getResultList());
	}

	@Transactional
	public Set<Standardlevel> findStandardlevelByDescriptionContaining(String description) throws DataAccessException {

		return findStandardlevelByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardlevel> findStandardlevelByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardlevelByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Standardlevel>(query.getResultList());
	}

	@Transactional
	public Standardlevel findStandardlevelByPrimaryKey(Integer id) throws DataAccessException {
		return findStandardlevelByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Standardlevel findStandardlevelByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardlevelByPrimaryKey", startResult, maxRows, id);
			return (Standardlevel) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standardlevel> findStandardlevelsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardlevelsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standardlevel> findAllStandardlevels() throws DataAccessException {

		return findAllStandardlevels(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardlevel> findAllStandardlevels(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllStandardlevels", startResult, maxRows);
		return new LinkedHashSet<Standardlevel>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardlevel> findStandardlevelByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Standardlevel>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Standardlevel> findStandardlevelsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Standardlevel.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Standardlevel> standardlevels = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return standardlevels;
	}

	@Transactional
	public Standardlevel findStandardlevelByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findStandardlevelByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Standardlevel findStandardlevelByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardlevelByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Standardlevel) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Standardlevel entity) {
		return true;
	}
}
